import 'package:doctor_login_lib/cache/userinfo_cache.dart';
import 'package:doctor_login_lib/login/entity/doctor_login_entity.dart'
    as LoginBean;
import 'package:flutter/cupertino.dart';
import 'package:doctor_login_lib/repository/http.dart';
import 'package:rxdart/rxdart.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/utils/regexp_ex.dart';
import '../component.dart';

class SetLoginPasswordBloc extends BaseBloc {

  PublishSubject<bool> loadingStatus = PublishSubject();

  String _pwd = '', _rePwd = '';

  @override
  void dispose() {
    loadingStatus?.close();
  }

  void editPwdChanged(String value) {
    _pwd = value;
  }

  void editRePwdChanged(String value) {
    _rePwd = value;
  }

  ///设置密码
  void setPwd(BuildContext context, phone) async {
    if (_pwd.isEmpty) {
      ToastUtil.show("请输入密码!");
      return;
    }

    if (!_pwd.isValidPassword()) {
      ToastUtil.show("密码由6-16位数字或字母组成");
      return;
    }

    if (_pwd != _rePwd) {
      ToastUtil.show("两次密码输入不一致!");
      return;
    }
    loadingStatus.add(true);
    var result = await LoginHttp.setPassword(phone, _pwd);
    loadingStatus.add(false);
    if (result.isSuccess) {
      var doctor = await UserInfoCache.getInstance().get();
      DoctorLoginComponent.dealLoginLogic(
          context,
          LoginBean.Data()
            ..setPersonPass = "0"
            ..doctorUser = doctor);
    } else {
      ToastUtil.show(result?.error?.msg ?? "");
    }
  }
}
